#include "app_config.h"

#define GET_CFG_VALUE_INT(cache_var , pnode ,node , default_val) \
{ assert(xml_cfg.IsOpen()) ; \
    if(cache_var.is_cached==true){return cache_var.val ; } \
    bool ok = false ; \
    std::string text = GetNodeText(pnode , node , &ok) ; \
    if(ok){ \
    cache_var.is_cached = true; \
    cache_var.val	= ConfigManager::Str2Int(text)  ; \
    return cache_var.val ; } \
else \
    return default_val ; }

#define GET_CFG_VALUE_DOUBLE(cache_var , pnode ,node , default_val) \
{ assert(xml_cfg.IsOpen()) ; \
    if(cache_var.is_cached==true){return cache_var.val ; } \
    bool ok = false ; \
    std::string text = GetNodeText(pnode , node , &ok) ; \
    if(ok){ \
    cache_var.is_cached = true; \
    cache_var.val	= ConfigManager::Str2Double(text)  ; \
    return cache_var.val ; } \
else \
    return default_val ; }

#define  GET_CFG_VALUE_STR(cache_val ,pnode, node , default_val) \
{ assert(xml_cfg.IsOpen()) ;  \
    if(cache_val.is_cached==true){return cache_val.val ;}\
    bool ok = false ;\
    std::string text = GetNodeText(pnode,node , &ok) ;\
    if(ok){\
    cache_val.is_cached = true; \
    cache_val.val	= text  ; \
    return text ; }else return default_val ; }

#define SET_CFG_VALUE_INT(cache_val ,pnode,node ,value ,save2file) \
     {\
     assert(xml_cfg.IsOpen()) ;  \
     cache_val.is_cached = true ; \
     cache_val.val = value ;  \
     char val_str[50] ; \
     sprintf_s(val_str , 50 , "%d" ,value ) ; \
     SetNodeText(pnode, node , val_str , save2file) ;\
   }

#define SET_CFG_VALUE_DOUBLE(cache_val ,pnode,node ,value ,save2file) \
     {\
     assert(xml_cfg.IsOpen()) ;  \
     cache_val.is_cached = true ; \
     cache_val.val = value ;  \
     char val_str[50] ; \
     sprintf_s(val_str , 50 , "%lf" ,value ) ; \
     SetNodeText(pnode, node , val_str , save2file) ;\
   }

#define SET_CFG_VALUE_STR(cache_val , pnode  ,node , value  , save2file) \
{ \
    assert(xml_cfg.IsOpen()) ;\
    cache_val.is_cached = true ;  \
    cache_val.val = value ; \
    SetNodeText(pnode, node  ,value , save2file) ;  \
}

app_config::app_config()
{
}

app_config::~app_config()
{
}

bool app_config::LoadConfigFile( std::string filename) 
{
    return xml_cfg.LoadConfigFile(filename) ;
}

void app_config::Save2File()
{
    xml_cfg.Save();
}

std::string  app_config::GetNodeText(std::string parent_node_name , std::string node_name ,bool* isOk) 
{
    TiXmlElement* ConfigNode =NULL ; 
    std::string ValueStr ;

    if(!xml_cfg.GetNodePointerByNameFromRoot(parent_node_name , ConfigNode) ) 
    {
        *isOk = false ; 
        return 0 ;
    }
    if(!xml_cfg.QueryNode_Text(ConfigNode ,node_name ,ValueStr) )
    {
        *isOk = false ; 
        return std::string() ;
    }

    *isOk = true ; 
    return ValueStr ; 
}

void   app_config::SetNodeText(std::string parent_node_name , std::string node_name ,  std::string text  , bool save2file ) 
{
    TiXmlElement* ConfigNode =NULL ; 
    std::string ValueStr ;

    if(!xml_cfg.GetNodePointerByNameFromRoot(parent_node_name , ConfigNode) )  return ; 
    xml_cfg.ModifyNode_Text(ConfigNode , node_name , text ) ; 
    if(save2file)
        xml_cfg.Save() ;
    return ; 
}

void app_config::set_ais_target_timeout(unsigned int timeout)
{
    SET_CFG_VALUE_INT(ais_target_timeout_, "timeout", "ais_target", timeout, false);
}

unsigned int app_config::get_ais_target_timeout()
{
    GET_CFG_VALUE_INT(ais_target_timeout_, "timeout", "ais_target", 300);
}

void app_config::set_ais_history_timeout(unsigned int timeout)
{
    SET_CFG_VALUE_INT(ais_history_timeout_, "timeout", "ais_history", timeout, false);
}

unsigned int app_config::get_ais_history_timeout()
{
    GET_CFG_VALUE_INT(ais_history_timeout_, "timeout", "ais_history", 300);
}

void app_config::set_ais_target_color(std::string color)
{
    SET_CFG_VALUE_STR(ais_target_color_, "color", "ais_target", color, false);
}

std::string app_config::get_ais_target_color()
{
    GET_CFG_VALUE_STR(ais_target_color_, "color", "ais_target", "");
}

void app_config::set_ais_history_color(std::string color)
{
    SET_CFG_VALUE_STR(ais_history_color_, "color", "ais_target_history", color, false);
}

std::string app_config::get_ais_history_color()
{
    GET_CFG_VALUE_STR(ais_history_color_, "color", "ais_target_history", "");
}

void app_config::set_radar_longitude(double val)
{
    SET_CFG_VALUE_DOUBLE(radar_longitude, "radar_info", "radar_longitude", val, false);
}

double app_config::get_radar_longitude()
{
    GET_CFG_VALUE_DOUBLE(radar_longitude, "radar_info", "radar_longitude", 121.0);
}

void app_config::set_radar_latitude(double val)
{
    SET_CFG_VALUE_DOUBLE(radar_latitude, "radar_info", "radar_latitude", val, false);
}

double app_config::get_radar_latitude()
{
    GET_CFG_VALUE_DOUBLE(radar_latitude, "radar_info", "radar_latitude", 37.0);
}

void app_config::set_radar_altitude(double val)
{
    SET_CFG_VALUE_DOUBLE(radar_altitude, "radar_info", "radar_altitude", val, false);
}

double app_config::get_radar_altitude()
{
    GET_CFG_VALUE_DOUBLE(radar_altitude, "radar_info", "radar_altitude", 100.0);
}

void app_config::set_radar_azimuth_modify(double val)
{
    SET_CFG_VALUE_DOUBLE(radar_azimuth_modify, "radar_info", "radar_azimuth_modify", val, true);
}

double app_config::get_radar_azimuth_modify()
{
    GET_CFG_VALUE_DOUBLE(radar_azimuth_modify, "radar_info", "radar_azimuth_modify", 0);
}

void app_config::set_radar_maxwave(double val)
{
    SET_CFG_VALUE_DOUBLE(radar_max_wave, "radar_info", "radar_maxwave", val, true);
}

double app_config::get_radar_maxwave()
{
    GET_CFG_VALUE_DOUBLE(radar_max_wave, "radar_info", "radar_maxwave", 300000.0);
}

void app_config::set_radar_range_modify(double val)
{
    SET_CFG_VALUE_DOUBLE(radar_range_modify, "radar_info", "radar_range_modify", val, true);
}

double app_config::get_radar_range_modify()
{
    GET_CFG_VALUE_DOUBLE(radar_range_modify, "radar_info", "radar_range_modify", 0);
}

int app_config::get_radar_type()
{
    GET_CFG_VALUE_INT(radar_type, "radar_info", "radar_type", 0);
}

void app_config::set_radar_target_color(std::string color)
{
    SET_CFG_VALUE_STR(radar_target_color_, "color", "radar_target", color, false);
}

std::string app_config::get_radar_target_color()
{
    GET_CFG_VALUE_STR(radar_target_color_, "color", "radar_target", "");
}

void app_config::set_radar_history_color(std::string color)
{
    SET_CFG_VALUE_STR(radar_history_color_, "color", "radar_target_history", color, false);
}

std::string app_config::get_radar_history_color()
{
    GET_CFG_VALUE_STR(radar_history_color_, "color", "radar_target_history", "");
}

void app_config::set_radar_target_timeout(unsigned int timeout)
{
    SET_CFG_VALUE_INT(radar_target_timeout_, "timeout", "radar", timeout, false);
}
unsigned int app_config::get_radar_target_timeout()
{
    GET_CFG_VALUE_INT(radar_target_timeout_, "timeout", "radar", 300);
}

void app_config::set_radar_history_timeout(unsigned int timeout)
{
    SET_CFG_VALUE_INT(radar_history_timeout_, "timeout", "radar_history", timeout, false);
}

unsigned int app_config::get_radar_history_timeout()
{
    GET_CFG_VALUE_INT(radar_history_timeout_, "timeout", "radar_history", 300);
}

void app_config::set_low_speed(double val)
{
    SET_CFG_VALUE_DOUBLE(low_speed_, "alarm", "low_speed", val, false);
}

double app_config::get_low_speed()
{
    GET_CFG_VALUE_DOUBLE(low_speed_, "alarm", "low_speed", 0);
}

void app_config::set_high_speed(double val)
{
    SET_CFG_VALUE_DOUBLE(high_speed_, "alarm", "high_speed", val, false);
}

double app_config::get_high_speed()
{
    GET_CFG_VALUE_DOUBLE(high_speed_, "alarm", "high_speed", 0);
}

void app_config::set_low_altitude(double val)
{
    SET_CFG_VALUE_DOUBLE(low_altitude_, "alarm", "low_altitude", val, false);
}

double app_config::get_low_altitude()
{
    GET_CFG_VALUE_DOUBLE(low_altitude_, "alarm", "low_altitude", 0);
}

void app_config::set_high_altitude(double val)
{
    SET_CFG_VALUE_DOUBLE(high_altitude_, "alarm", "high_altitude", val, false);
}

double app_config::get_high_altitude()
{
    GET_CFG_VALUE_DOUBLE(high_altitude_, "alarm", "high_altitude", 0);
}

void app_config::set_trail_level(unsigned int level)
{
    SET_CFG_VALUE_INT(trail_level_, "radar_info", "radar_trail_level", level, false);
}

unsigned int app_config::get_trail_level()
{
    GET_CFG_VALUE_INT(trail_level_, "radar_info", "radar_trail_level", 0);
}

void app_config::set_enable_sea_filter(unsigned int val)
{
    SET_CFG_VALUE_INT(enable_sea_filter_, "radar_info", "sea_filter", val, false);
}

unsigned int app_config::get_enable_sea_filter()
{
    GET_CFG_VALUE_INT(enable_sea_filter_, "radar_info", "sea_filter", 0);
}

void app_config::set_compass_valid(unsigned int val)
{
    SET_CFG_VALUE_INT(compass_valid_, "radar_info", "compass_valid", val, false);
}

unsigned int app_config::get_compass_valid()
{
    GET_CFG_VALUE_INT(compass_valid_, "radar_info", "compass_valid", 0);
}

void app_config::set_compass_port(unsigned int val)
{
    SET_CFG_VALUE_INT(compass_port_, "radar_info", "compass_port", val, false);
}

unsigned int app_config::get_compass_port() 
{
    GET_CFG_VALUE_INT(compass_port_, "radar_info", "compass_port", 0);
}

void app_config::set_compass_ip(const std::string& val)
{
    SET_CFG_VALUE_STR(compass_ip_, "radar_info", "compass_ip", val, false);
}

std::string app_config::get_compass_ip() 
{
    GET_CFG_VALUE_STR(compass_ip_, "radar_info", "compass_ip", "");
}

void app_config::set_output_type(unsigned int val)
{
    SET_CFG_VALUE_INT(output_type_, "output", "type", val, false);
}

unsigned int app_config::get_output_type()
{
    GET_CFG_VALUE_INT(output_type_, "output", "type", 0);
}

void app_config::set_output_port(unsigned int val)
{
    SET_CFG_VALUE_INT(output_port_, "output", "port", val, false);
}

unsigned int app_config::get_output_port()
{
    GET_CFG_VALUE_INT(output_port_, "output", "port", 0);
}

void app_config::set_output_flag(unsigned int val)
{
    SET_CFG_VALUE_INT(output_flag_, "output", "validflag", val, false);
}

unsigned int app_config::get_output_flag()
{
    GET_CFG_VALUE_INT(output_flag_, "output", "validflag", 0);
}

void app_config::set_output_ip(const std::string& val)
{
    SET_CFG_VALUE_STR(output_ip_, "output", "ip", val, false);
}

std::string app_config::get_output_ip()
{
    GET_CFG_VALUE_STR(output_ip_, "output", "ip", "");
}

void app_config::set_ivef_user_name(const std::string& val)
{
    SET_CFG_VALUE_STR(ivef_user_name_, "output", "username", val, false);
}

std::string app_config::get_ivef_user_name()
{
    GET_CFG_VALUE_STR(ivef_user_name_, "output", "username", "");
}

void app_config::set_ivef_password(const std::string& val)
{
    SET_CFG_VALUE_STR(ivef_password_, "output", "password", val, false);
}

std::string app_config::get_ivef_password()
{
    GET_CFG_VALUE_STR(ivef_password_, "output", "password", "");
}

void app_config::set_ivef_auto_login(unsigned int val)
{
    SET_CFG_VALUE_INT(ivef_auto_login_, "output", "autologin", val, false);
}

unsigned int app_config::get_ivef_auto_login()
{
    GET_CFG_VALUE_INT(ivef_auto_login_, "output", "autologin", 0);
}

void app_config::set_widget_style(unsigned int val)
{
    SET_CFG_VALUE_INT(ui_widget_style_, "app", "style", val, true);
}

unsigned int app_config::get_widget_style()
{
    GET_CFG_VALUE_INT(ui_widget_style_, "app", "style", 0);
}
